﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Southridge.XmlData;

namespace Southridge.Data
{

    public class Preference : BusinessEntity 
    {
        protected int _MinRooms;
        public int MinRooms
        {
            get
            {
                return _MinRooms;
            }
            set
            {
                if (_MinRooms != value)
                {
                    _MinRooms = value;
                    OnPropertyChanged("MinRooms");
                }
            }
        }

        protected int _YearBuilt;
        public int YearBuilt
        {
            get
            {
                return _YearBuilt;
            }
            set
            {
                if (_YearBuilt != value)
                {
                    _YearBuilt = value;
                    OnPropertyChanged("YearBuilt");
                }
            }
        }

        protected double _MinBaths;
        public double MinBaths
        {
            get
            {
                return _MinBaths;
            }
            set
            {
                if (_MinBaths != value)
                {
                    _MinBaths = value;
                    OnPropertyChanged("MinBaths");
                }
            }
        }

        protected double _SquareFootage;
        public double SquareFootage
        {
            get
            {
                return _SquareFootage;
            }
            set
            {
                if (_SquareFootage != value)
                {
                    _SquareFootage = value;
                    OnPropertyChanged("SquareFootage");
                }
            }
        }

        protected double _Acreage;
        public double Acreage
        {
            get
            {
                return _Acreage;
            }
            set
            {
                if (_Acreage != value)
                {
                    _Acreage = value;
                    OnPropertyChanged("Acreage");
                }
            }
        }

        protected int _MinGarage;
        public int MinGarage
        {
            get
            {
                return _MinGarage;
            }
            set
            {
                if (_MinGarage != value)
                {
                    _MinGarage = value;
                    OnPropertyChanged("MinGarage");
                }
            }
        }

        protected bool _HasView;
        public bool  HasView
        {
            get
            {
                return _HasView;
            }
            set
            {
                if (_HasView != value)
                {
                    _HasView = value;
                    OnPropertyChanged("HasView");
                }
            }
        }

        protected bool _HasWaterFront;
        public bool HasWaterFront
        {
            get
            {
                return _HasWaterFront;
            }
            set
            {
                if (_HasWaterFront != value)
                {
                    _HasWaterFront = value;
                    OnPropertyChanged("HasWaterFront");
                }
            }
        }

        protected int _PriceStart;
        public int PriceStart
        {
            get
            {
                return _PriceStart;
            }
            set
            {
                if (_PriceStart != value)
                {
                    _PriceStart = value;
                    OnPropertyChanged("PriceStart");
                }
            }
        }

        protected int _PriceStop;
        public int PriceStop
        {
            get
            {
                return _PriceStop;
            }
            set
            {
                if (_PriceStop != value)
                {
                    _PriceStop = value;
                    OnPropertyChanged("PriceStop");
                }
            }
        }

        protected County _County; 
        public County County
        {
            get
            {
                return _County;
            }
            set
            {
                if (_County != value)
                {
                    _County = value;
                    OnPropertyChanged("County");
                }
            }
        }

        protected State _State;
        public State State
        {
            get
            {
                return _State;
            }
            set
            {
                if (_State != value)
                {
                    _State = value;
                    OnPropertyChanged("State");
                }
            }
        }

        protected PropertyType _PropertyType   ;
        public PropertyType PropertyType
        {
            get
            {
                return _PropertyType;
            }
            set
            {
                if (_PropertyType != value)
                {
                    _PropertyType = value;
                    OnPropertyChanged("PropertyType");
                }
            }
        }

        
        protected Neighborhood _Neighborhood;
        public Neighborhood Neighborhood
        {
            get
            {
                return _Neighborhood;
            }
            set
            {
                if (_Neighborhood != value)
                {
                    _Neighborhood = value;
                    OnPropertyChanged("Neighborhood");
                }
            }
        }


    }

    public class Profile : BusinessEntity, IDataErrorInfo 
    {

        public Profile()
        {
            this.Preference = new Preference();
            IsDirty = false; 
        } 
        protected string _firstName; 
        public string FirstName
        {
            get
            {
                return _firstName ; 
            }
            set
            {
                if ( _firstName != value  ) 
                { 
                    _firstName = value ; 
                    OnPropertyChanged ( "FirstName")  ; 
                } 
            }
        }

        protected string _LastName;
        public string LastName
        {
            get
            {
                return _LastName;
            }
            set
            {
                if (_LastName != value)
                {
                    _LastName = value;
                    OnPropertyChanged("LastName");
                }
            }
        }

        protected string _Email;
        public string Email
        {
            get
            {
                return _Email;
            }
            set
            {
                if (_Email != value)
                {
                    _Email = value;
                    OnPropertyChanged("Email");
                }
            }
        }


        protected string _HomePhoneNo;
        public string HomePhoneNo
        {
            get
            {
                return _HomePhoneNo;
            }
            set
            {
                if (_HomePhoneNo != value)
                {
                    _HomePhoneNo = value;
                    OnPropertyChanged("HomePhoneNo");
                }
            }
        }

        protected string _MobilePhoneNo;
        public string MobilePhoneNo
        {
            get
            {
                return _MobilePhoneNo;
            }
            set
            {
                if (_MobilePhoneNo != value)
                {
                    _MobilePhoneNo = value;
                    OnPropertyChanged("MobilePhoneNo");
                }
            }
        }

        protected string _Address1;
        public string Address1
        {
            get
            {
                return _Address1;
            }
            set
            {
                if (_Address1 != value)
                {
                    _Address1 = value;
                    OnPropertyChanged("Address1");
                }
            }
        }

        protected string _Address2;
        public string Address2
        {
            get
            {
                return _Address2;
            }
            set
            {
                if (_Address2 != value)
                {
                    _Address2 = value;
                    OnPropertyChanged("Address2");
                }
            }
        }


        protected string _City;
        public string City
        {
            get
            {
                return _City;
            }
            set
            {
                if (_City != value)
                {
                    _City = value;
                    OnPropertyChanged("City");
                }
            }
        }
        protected string _ZipCode;
        public string ZipCode
        {
            get
            {
                return _ZipCode;
            }
            set
            {
                if (_ZipCode != value)
                {
                    _ZipCode = value;
                    OnPropertyChanged("ZipCode");
                }
            }
        }

        protected State _State;
        public State State
        {
            get
            {
                return _State;
            }
            set
            {
                if (_State != value)
                {
                    _State = value;
                    OnPropertyChanged("State");
                }
            }
        }
        protected Preference _Preference;
        public Preference Preference
        {
            get
            {
                return _Preference;
            }
            set
            {
                if (_Preference != value)
                {
                    _Preference = value;
                    OnPropertyChanged("Preference");
                }
            }
        }

        protected string _PrimaryPhoto;
        public string PrimaryPhoto
        {
            get
            {
                return _PrimaryPhoto;
            }
            set
            {
                if (_PrimaryPhoto != value)
                {
                    _PrimaryPhoto = value;
                    OnPropertyChanged("PrimaryPhoto");
                }
            }
        }

        public int calls = 0; 

        public bool SimpleSave() 
        {
            return (++calls % 2 == 0); 
                 
        
        }


        #region IDataErrorInfo Members

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        System.Text.RegularExpressions.Regex digitsOnly =
              new System.Text.RegularExpressions.Regex(@"^\d{5}(?:-\d{4})?$");

        public override void Validate()
        {
            base.Validate();
            string s = this["ZipCode"];
            s = this["FirstName"];
            s = this["HomePhoneNo"];
            s = this["Address1"]; 
        }
        public string this[string columnName]
        {
            get
            {
                _errors.Remove(columnName);

                switch (columnName)
                {

                    case "FirstName":
                        {
                            if (string.IsNullOrEmpty (this.FirstName) )
                            {
                                _errors.Add(columnName, Resources.FirstNameRequired);
                                return Resources.FirstNameRequired;
                            }
                        }
                        break;
                    case "LastName":
                        {
                            if (string.IsNullOrEmpty(this.LastName) )
                            {
                                _errors.Add(columnName, Resources.LastNameRequired);
                                return Resources.LastNameRequired;
                            }
                        }
                        break;

                    case "HomePhoneNo":
                        {
                            if (string.IsNullOrEmpty(this.HomePhoneNo))
                            {
                                _errors.Add(columnName, Resources.HomePhoneNoRequired);
                                return Resources.HomePhoneNoRequired;
                            }
                        }
                        break;

                    case "Address1":
                        {
                            if ( string.IsNullOrEmpty(this.Address1) )
                            {
                                _errors.Add(columnName, Resources.AddressRequired);
                                return Resources.AddressRequired;
                            }
                        }
                        break;

                    case "ZipCode":
                        {
                            if ( string.IsNullOrEmpty(ZipCode) ||  !digitsOnly.IsMatch(ZipCode))
                            {
                                _errors.Add(columnName, Resources.InvalidZipCode);
                                return (Resources.InvalidZipCode);
                            }
                        }
                        break; 
                }


                return "";
            }
        }
      
        #endregion

        public override bool IsDirty
        {
            get
            {
                return base.IsDirty;
            }
            internal set
            {
                base.IsDirty = value;
            }
        }
    }
}
